<?php
namespace Home\Controller;
use Think\Cache;
use Think\Controller;
class MemberController extends CommonController {
    //员工列表
    public function index(){
        $storeInfo = M("store")->where(array("store_id"=>$this->sessionInfo['user_id']))->find();

        $relationship = M("store_relationship");
        $member = M("member");

        if($_POST['keyword']){
            $keyword = trim($_POST['keyword']);
            $where1['ecm_member.real_name|ecm_member.phone_mob'] = array("like","%$keyword%");
            $userInfo = $member->where($where1)->find();
            if(!empty($userInfo)){
                $con['ecm_store_relationship.son_id'] =$userInfo['user_id'];
            }
        }
        //说明登录人为总店铺
        if($storeInfo['parent_id'] == 0){
            $con['ecm_store_relationship.parent_id'] = $this->sessionInfo['user_id'];
        }else{
            $con['ecm_store_relationship.store_id'] = $this->sessionInfo['user_id'];
        }

        $count = $relationship
            ->join("join ecm_member on ecm_store_relationship.son_id = ecm_member.user_id")
            ->join("join ecm_store on ecm_store_relationship.parent_id = ecm_store.store_id")
            ->where($con)->count();
        $page = I('p') ? I('p') : '1';
        $pageSize = I('pageSize') ? I('pageSize') : 20;
        $list = $relationship
            ->join("join ecm_member on ecm_store_relationship.son_id = ecm_member.user_id")
            ->join("join ecm_store on ecm_store_relationship.parent_id = ecm_store.store_id")
            ->field("ecm_member.*,ecm_store.store_name,ecm_store.store_id,ecm_store_relationship.image,ecm_store_relationship.department_name")
            ->where($con)->page($page,$pageSize)->select();
        if(!empty($list)){
            //昨日开始和结束时间
            $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
            $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
            //今天开始时间
            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            $yesterdayWhere['reg_time'] = array("between",array($beginYesterday,$endYesterday));
            $todayWhere['reg_time'] = array("between",array($beginToday,$endToday));
            foreach($list as $k=>$v){
                $yesterdayWhere['fourth_class'] = $v['user_id'];
                $todayWhere['fourth_class'] = $v['user_id'];
                $where['fourth_class'] = $v['user_id'];
                $list[$k]['yesterday'] = $member->where($yesterdayWhere)->count();
                $list[$k]['today'] = $member->where($todayWhere)->count();
                $list[$k]['total'] = $member->where($where)->count();
            }
        }
        $show       = $this->getPage($count,$page,$pageSize);// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('list',$list);
        $this->display();
    }
    //编辑员工信息
    public function edit(){
        $memberModel = M("member");
        $store_relationshipModel = M("store_relationship");
        $storeModel = M("store");
        if(IS_POST){
            $post = $_POST;
            $where['user_id'] = array('neq',$post['user_id']);
            $where['user_name'] = $post['phone_mob'];
            $save = M("member")->where($where)->find();
            if(!$save){
                $post['password'] = 'wf123456';
                $post['confirm_password'] = 'wf123456';
                $result = $this->postInsert($post);
                if($result['code'] == 1){
                    $this->success('保存成功');
                }else{
                    $this->error($result['msg']);
                }
            }else{
                $this->error('该手机号已注册');
            }

        }else{
            $id = I("son_id");
            if($id){
                $relationshipInfo = $store_relationshipModel->where(array("son_id"=>$id))->find();
                if($relationshipInfo){
                    $relationshipInfo['userInfo'] = $memberModel->where(array('user_id'=>$id))->find();
                    $this->assign('relationshipInfo',$relationshipInfo);
                    $info = $storeModel->where(array('store_id'=>$this->sessionInfo['user_id']))->find();
                    //判断是否为总店铺
                    if($info['parent_id'] == 0){
                        $map['store_id']  = $this->sessionInfo['user_id'];
                        $map['parent_id']  = $this->sessionInfo['user_id'];
                        $map['_logic'] = 'or';
                        $where['_complex'] = $map;
                        $storeInfo = $storeModel->where($where)->select();
                        $this->assign('storeList',$storeInfo);
                    }
                    $filter['store_id'] = $relationshipInfo['store_id'];
                    $list = D('store_department')->where($filter)->select();
                    $this->assign('list',$list);
                    $this->assign('info',$info);
                    $this->display();
                }else{
                    $this->error('员工信息错误');
                }
            }else{
                $this->error('信息传输错误');
            }
        }
    }

    //添加店铺员工
    public function insert()
    {
        $storeModel = M("store");
        $memberModel = M("member");
        $storeRelationshipModel = M("store_relationship");

        $sessionInfo = $_SESSION['seller'];
        if(IS_POST){
            $post = $_POST;
            $post['password'] = 'wf123456';
            $post['confirm_password'] = 'wf123456';
            $result = $this->postInsert($post);
            if($result['code'] == 1){
                $this->success('添加成功');
            }else{
                $this->error($result['msg']);
            }
        }else{
            $info = $storeModel->where(array('store_id'=>$sessionInfo['user_id']))->find();
            //判断是否为总店铺
            if($info['parent_id'] == 0){
                $map['store_id']  = $this->sessionInfo['user_id'];
                $map['parent_id']  = $this->sessionInfo['user_id'];
                $map['_logic'] = 'or';
                $where['_complex'] = $map;
                $storeInfo = $storeModel->where($where)->select();
                $this->assign('storeList',$storeInfo);
            }
            $filter['store_id'] = $this->sessionInfo['user_id'];
            $list = D('store_department')->where($filter)->select();
            $this->assign('list',$list);
            $this->assign('info',$info);
            $this->display();
        }
    }

    //添加数据
    public function postInsert($post){

        $memberModel = D("member");
        $storeModel = D("store");
        $storeRelationshipModel = D("store_relationship");
        $code = -1;
        $msg = '未填写用户名';
        if($post['real_name']){
//            $msg = '未填写密码';
//            if($post['password']){
//                $msg = '未填写确认密码';
//                if($post['confirm_password']){
//                    $msg = '未选择店铺';
//                    if($post['store_id']){
//                        $msg = '两次密码不一致';
//                        if($post['password'] == $post['confirm_password']){
                            $msg = '手机号格式错误';
                            $validatePhone = $this->check_mobile($post['phone_mob']);
                            if($validatePhone){
                                //防止乱填手机号，进行多一步验证
                                if($post['user_id']){
                                    $where['user_id'] = $post['user_id'];
                                }else{
                                    $where['phone_mob']  = $post['phone_mob'];
                                    $where['user_name']  = $post['phone_mob'];
                                }
                                $memberInfo = $memberModel->where($where)->find();


                                $storeInfo = $storeModel->where(array('store_id' => $post['store_id']))->find();
                                if ($storeInfo['parent_id'] == 0) {
                                    $parent_id = $storeInfo['store_id'];
                                    $second_id = 0;
                                } else {
                                    $parent_id = $storeModel->where(array('store_id' => $storeInfo['parent_id']))->getField('store_id');
                                    $second_id = $storeInfo['store_id'];
                                }
                                $logopath = SITE_HOST . '/' . $storeInfo['store_logo'];
                                $storeModel->startTrans();
                                $time = time();
                                $data = array(
                                    'user_name' => $post['phone_mob'],
                                    'real_name' => $post['real_name'],
                                    'password' => md5($post['password']),
                                    'phone_mob' => $post['phone_mob'],
                                    'email' => $post['email'],
                                    'reg_time' => $time,
                                    's_id' => $storeInfo['store_id'],
                                    's_name' => $storeInfo['store_name'],
                                    'first_class' => $parent_id,
                                    'second_class' => $second_id
                                );
                                if(empty($memberInfo)) {
                                    $info = $memberModel->add($data);

                                    $couponSettingModel = M("coupon_setting");
                                    $memberCouponModel = M('member_coupon');
                                    $register_coupon =$couponSettingModel->where('setting_id = 4')->find();
                                    $arr_coupon = array(
                                        'coupon_name' => $register_coupon['name'],
                                        'remark' => $register_coupon['remark'],
                                        'money' => $register_coupon['money'],
                                        'min_money' => $register_coupon['min_money'],
                                        'user_id' => $info,
                                        'start_time' => $time,
                                        'end_time' => $time+$register_coupon['expiry_time'],
                                        'is_used' => 0,
                                    );
                                    $add_register_coupon = $memberCouponModel->add($arr_coupon);

                                }else{
                                    $save = $memberModel->where(array('user_id'=>$memberInfo['user_id']))->save($data);
                                    $info = $memberInfo['user_id'];
                                }
                                if ($info != false) {
                                    //增加token标识
                                    $token = md5('user_' . $info . '_' . '_' . time());
                                    M('AppToken')->add(array(
                                        'user_id' => $info,
                                        'token' => $token
                                    ));
//                                    if(!$res['qr_code']){
                                        $dataEncrypt = $this->set_encrypt($info);
                                        $url = SITE_HOST.'/mobile/index.php?app=qrcode&act=download&invite_code=' . $dataEncrypt;
                                        $pash = './uploads/qrcode/' . $info . '.png';
                                        $path = $this->create_png($url, false, $pash, 'H',10,6,false,'');
//                                        $path = $this->create_png($url, false, $pash, 'H',10,6,false,'',$storeInfo['store_name'],$data['real_name']);
//                                    }
//                                    //生成二维码
//                                    $url = 'http://www.jingjing.shop/mobile/index.php?app=share&act=index&token=' . $token;
//                                    $pash = './uploads/qrcode/' . $info . '.png';
//                                    $path = $this->create_png($url, false, $pash, 'H', '', '', '', $logopath);
                                    //查询是否有部门的存在
                                    if ($post['department_id'] && $post['department_id'] !=0) {
                                        $departmentInfo = D("store_department")->where(array('department_id' => $post['department_id']))->find();
                                        if (empty($departmentInfo)) {
                                            $msg = '部门信息不存在';
                                            $storeModel->rollback();
                                            return array('code' => $code, 'msg' => $msg);
                                        }
                                    }

                                    $storeRelationshipInfo = $storeRelationshipModel->where(array('son_id'=>$info))->find();
                                    //添加到数据库
                                    $relationArr = array(
                                        'son_id' => $info,
                                        'store_id' => $storeInfo['store_id'],
                                        'parent_id' => $parent_id,
                                        'image' => $path,
                                        'department_id' => $departmentInfo['department_id'] ? $departmentInfo['department_id'] : 0,
                                        'department_name' => $departmentInfo['department_name'] ? $departmentInfo['department_name'] : '',
                                    );
                                    if(!empty($storeRelationshipInfo)){
                                        $addRelation = $storeRelationshipModel->where(array('son_id'=>$info))->save($relationArr);
                                    }else{
                                        $addRelation = $storeRelationshipModel->add($relationArr);
                                    }

                                    $save = $memberModel->where(array('user_id'=>$info))->save(array("third_class"=>$info,'fourth_class'=>0));
                                    $storeModel->commit();
                                    $code = 1;
                                    $msg = '添加成功';
                                } else {
                                    $storeModel->rollback();
                                    $msg = '添加失败';
                                }
                            }
//                        }
//                    }
//                }
//            }
        }
        return array('code'=>$code,'msg'=>$msg);
    }

    public function qrcode($id ,$token){
        require_once '../promotion/ThinkPHP/Library/Vendor/phpqrcode/phpqrcode.php';
        $qrcode = new \QRcode();
        $url = SITE_HOST.'/mobile/index.php?app=share&act=index&token='.$token;
        $pash = './uploads/qrcode/'.$id. '.png';
        if (!file_exists(dirname($pash))){
            mkdir(dirname($pash),0777,true);
        }
        if(file_exists($pash)){
            unlink($pash);
        }
        $qrcode::png($url, $pash, 'H', 10,2);
//        $path = substr($pash, 1);
        return $pash;
    }

//    public function create_png($text, $bool = false, $outfile = false, $level = 'H', $size = 10, $margin = 2, $saveandprint=false,$logopath)
//    {
//        require_once '../promotion/ThinkPHP/Library/Vendor/phpqrcode/phpqrcode.php';
//        $qrcode = new \QRcode();
//        if ( $outfile ) {
//            if (!file_exists(dirname($outfile))){
//                mkdir(dirname($outfile),0777,true);
//            }
//            $qrcode::png($text, $outfile, $level, $size, $margin, $saveandprint);
//            $QR = file_get_contents($outfile);
//        } else {
//            ob_start();
//            $qrcode::png($text, $outfile, $level, $size, $margin, $saveandprint);
//            $QR = ob_get_contents();
//            ob_end_clean();
//        }
//
//        //判断是否生成带logo的二维码
//        if($bool)
//        {
//            $QR = imagecreatefromstring(file_get_contents($outfile));        //目标图象连接资源。
//            $logo = imagecreatefromstring(file_get_contents($logopath));    //源图象连接资源。
//
//            $QR_width = imagesx($QR);            //二维码图片宽度
//            $QR_height = imagesy($QR);            //二维码图片高度
//            $logo_width = imagesx($logo);        //logo图片宽度
//            $logo_height = imagesy($logo);        //logo图片高度
//
//            $logo_qr_width = $QR_width / 4;       //组合之后logo的宽度(占二维码的1/5)
//            $scale = $logo_width/$logo_qr_width;       //logo的宽度缩放比(本身宽度/组合后的宽度)
//            $logo_qr_height = $logo_height/$scale;  //组合之后logo的高度
//            $from_width = ($QR_width - $logo_qr_width) / 2;   //组合之后logo左上角所在坐标点
//            //重新组合图片并调整大小
//            //imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
//            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
//
//            //输出图片
//            imagepng($QR, $outfile);
//            imagedestroy($QR);
//            imagedestroy($logo);
//        }
//        return $outfile;
//    }

    public function excel(){

        $storeInfo = M("store")->where(array("store_id"=>$this->sessionInfo['user_id']))->find();
        $relationship = M("store_relationship");
        $member = M("member");
        $con = array(
            'ecm_store_relationship.store_id' => $this->sessionInfo['user_id'] ,
        );
        //说明登录人为总店铺
        if($storeInfo['parent_id'] == 0){
            $con = array('ecm_store_relationship.parent_id'=>$this->sessionInfo['user_id']);
        }

        $list = $relationship
            ->join("join ecm_member on ecm_store_relationship.son_id = ecm_member.user_id")
            ->join("join ecm_store on ecm_store_relationship.parent_id = ecm_store.store_id")
            ->field("ecm_member.*,ecm_store.store_name,ecm_store_relationship.image,ecm_store_relationship.department_name")
            ->where($con)->select();
        if(!empty($list)){
            require '../sellerserver/ThinkPHP/Library/Vendor/PHPExcel/PHPExcel.php';
            $excel = new \PHPExcel();
            $objActSheet = $excel->getActiveSheet();
            $objActSheet->setCellValue('A1', '员工名称');
            $objActSheet->setCellValue('B1', '所属店铺');
            $objActSheet->setCellValue('C1', '所属部门');
            $objActSheet->setCellValue('D1', '注册时间');
            $objActSheet->setCellValue('E1', '二维码');

            foreach ($list as $k => $v){
                $k +=2;
                $time = date('Y-m-d H:i:s',$v['reg_time']);
                $objActSheet->setCellValue('A'.$k, $v['real_name']);
                $objActSheet->setCellValue('B'.$k, $v['s_name']);
                $objActSheet->setCellValue('C'.$k, $v['department_name']?$v['department_name']:'无');
                $objActSheet->setCellValue('D'.$k, $time);
                if ($v['image']){
                    $objDrawing = new \PHPExcel_Worksheet_Drawing(); //必须每次重新实例化
                    $objDrawing->setPath($v['image']);//这里是相对路径
                    $objDrawing->setHeight(80);//照片高度
                    $objDrawing->setWidth(80);
                    $objDrawing->setCoordinates('E'.$k);
                    // 图片偏移距离
                    $objDrawing->setOffsetX(12);
                    $objDrawing->setOffsetY(12);
                    $objDrawing->setWorksheet($excel->getActiveSheet());
                }
                $objActSheet->getRowDimension($k)->setRowHeight(80);//设置行高     放在前面设置有数据的行不生效
                $objActSheet->getColumnDimension('D')->setWidth(40);//设置G行宽度  放在前面设置有数据的行不生效
                $objActSheet->getColumnDimension('E')->setWidth(40);//设置G行宽度  放在前面设置有数据的行不生效
            }
            $excel = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');  //Excel2007  兼容好
            ob_end_clean();   //解决乱码
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename=导出列表' . date('Ymd') . '.xlsx');
            header('Cache-Control: max-age=0');
            $excel->save('php://output');
            exit;
        }else{
            $this->error('所选数据为空！');
        }
    }

    //导出员工推广详细
    public function excelMember(){
        $store_id = I("get.store_id",0,'intval');
        if(!$store_id){
            $this->error('信息错误');
        }
        $member = M("member");
//        $where['third_class'] = $store_id;
        $where['fourth_class'] = $store_id;

        $list = $member->where($where)->order('reg_time desc')->select();
        if(!empty($list)){
            foreach($list as $k=>$v){
                $list[$k]['third_name'] = $member->where(array('user_id'=>$v['third_class']))->getField("real_name");
                $list[$k]['second_name'] = $member->where(array('user_id'=>$v['second_class']))->getField("user_name");
            }

            require '../sellerserver/ThinkPHP/Library/Vendor/PHPExcel/PHPExcel.php';
            $excel = new \PHPExcel();

            $objActSheet = $excel->getActiveSheet();
            $objActSheet->setCellValue('A1', '用户名');
            $objActSheet->setCellValue('B1', '手机号');
            $objActSheet->setCellValue('C1', '推广人');
            $objActSheet->setCellValue('D1', '所属店铺');
            $objActSheet->setCellValue('E1', '注册时间');

            foreach ($list as $k => $v){
                $k +=2;
                $time = date('Y-m-d H:i:s',$v['reg_time']);
                $objActSheet->setCellValue('A'.$k, $v['user_name']);
                $objActSheet->setCellValue('B'.$k, $v['phone_mob']);
                $objActSheet->setCellValue('C'.$k, $v['third_name']);
                $objActSheet->setCellValue('D'.$k, $v['second_name']);
                $objActSheet->setCellValue('E'.$k, $time);
            }
            $excel = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');  //Excel2007  兼容好
            ob_end_clean();   //解决乱码
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename=员工推广详细' . date('Ymd') . '.xlsx');
            header('Cache-Control: max-age=0');
            $excel->save('php://output');
            exit;
        }else{
            $this->error('您所选的数据为空！');
        }


    }
    //导入页面
    public function importExcel(){
        $storeModel = D("store");
        $info = $storeModel->where(array('store_id'=>$this->sessionInfo['user_id']))->find();

        //判断是否为总店铺
        if($this->sessionInfo['parent_id'] == 0){
            $map['store_id']  = $this->sessionInfo['user_id'];
            $map['parent_id']  = $this->sessionInfo['user_id'];
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
            $storeInfo = $storeModel->where($where)->select();
            $this->assign('storeList',$storeInfo);
        }
        $filter['store_id'] = $this->sessionInfo['user_id'];
        $list = D('store_department')->where($filter)->select();
        if(S("newArr")){
            $newArr = S("newArr");
            S("newArr",null);
            require '../sellerserver/ThinkPHP/Library/Vendor/PHPExcel/PHPExcel.php';
            $excel = new \PHPExcel();
            $objActSheet = $excel->getActiveSheet();
            $objActSheet->setCellValue('A1', '员工姓名');
            $objActSheet->setCellValue('B1', '手机号');
            $objActSheet->setCellValue('C1', '邮箱');
            $objActSheet->setCellValue('D1', '错误原因');

            $arrData = array_values($newArr);
            foreach ($arrData as $k => $v){
                $k +=2;
                $objActSheet->setCellValue('A'.$k, $v['real_name']);
                $objActSheet->setCellValue('B'.$k, $v['phone_mob']);
                $objActSheet->setCellValue('C'.$k, $v['email']);
                $objActSheet->setCellValue('D'.$k, $v['msg']);
                $objActSheet->getColumnDimension('A')->setWidth(30);//设置G行宽度  放在前面设置有数据的行不生效
                $objActSheet->getColumnDimension('B')->setWidth(30);//设置G行宽度  放在前面设置有数据的行不生效
                $objActSheet->getColumnDimension('C')->setWidth(30);//设置G行宽度  放在前面设置有数据的行不生效
                $objActSheet->getColumnDimension('D')->setWidth(40);//设置G行宽度  放在前面设置有数据的行不生效
                $objActSheet->getColumnDimension('E')->setWidth(20);//设置G行宽度  放在前面设置有数据的行不生效
                $objActSheet->getColumnDimension('F')->setWidth(80);//设置G行宽度  放在前面设置有数据的行不生效
            }
            $excel = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');  //Excel2007  兼容好
            ob_end_clean();   //解决乱码
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename=导入列表错误信息回馈' . date('Ymd') . '.xlsx');
            header('Cache-Control: max-age=0');
            $excel->save('php://output');
        }
        $this->assign('info',$info);
        $this->assign('list',$list);
        $this->display();
    }
    //导入员工数据
    public function postImportExcel(){
        require '../sellerserver/ThinkPHP/Library/Vendor/PHPExcel/PHPExcel/IOFactory.php';

        if(!$_FILES['file']){
            $this->error('请上传文件');
        }
        if(!$_POST['store_id']){
            $this->error('请选择店铺');
        }
        $years = date('Y-m-d');

        $pash = './uploads/excel/'.$years.'/'.$years.'_request_log.txt';
        $dir_name = dirname($pash);
        if (!file_exists($dir_name)){
            mkdir(dirname($pash),0777,true);
        }
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('xls', 'jpg', 'gif', 'png', 'jpeg' ,'txt', 'php', 'doc', 'xlsx','css');// 设置附件上传类型
        $upload->rootPath  =     './uploads/excel/'.$years.'/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        $upload->autoSub  =     false; // 设置附件上传（子）目录
        $upload->saveName = 'time';
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            $file_name = './uploads/excel/'.$years.'/'.$info['file']['savename'];
            $obj_PHPExcel = \PHPExcel_IOFactory::load($file_name);
            $excel_array = $obj_PHPExcel->getsheet(0)->toArray();
            array_shift($excel_array);  //删除第一个数组(标题);
            $arr = array_values($excel_array);

            $data = array();
            $member = D("member");
            $ids = '';
            foreach ($arr as $key => $value){
//                if($key == 0){
//                    $ids = "'".$value[0]."'";
//                }else{
//                    $ids .= ",'".$value[0]."'";
//                }
                $data['real_name'] = $value[0];
                $data['user_name'] = $value[1];
                $data['password']  = 'wf123456';
                $data['confirm_password']  = 'wf123456';
                $data['phone_mob'] = $value[1];
                $data['email']     = $value[2]?$value[2]:'';
                $data['store_id']  = $_POST['store_id'];
                $data['department_id'] = $_POST['department_id'];
                $result = $this->postInsert($data);
                if($result['code'] == -1){
                    $newArr[$key]['real_name'] = $data['real_name'];
                    $newArr[$key]['phone_mob'] = $data['phone_mob'];
                    $newArr[$key]['email'] = $data['email'];
                    $newArr[$key]['msg'] = $result['msg'];
                    continue;
                }
            }
//            dump($ids);die;
            if(count($newArr) >= 1){
                S('newArr',$newArr);
                $this->success('导入成功，但部分数据错误');
            }else{
                $this->success('全部导入成功');
            }
        }
    }

    //请求拿到店铺的部门
    public function getDepartmentList(){
        $store_id = $_POST['store_id'];
        $list = D("store_department")->where(array('store_id'=>$store_id))->select();
        if(!empty($list)){
            $arr['code'] = 1;
            $arr['data'] = $list;
        }else{
            $arr['code'] = -1;
        }
        echo json_encode($arr);
        exit();
    }


    public function editPassword(){
        if(IS_POST){
            $store = D("store");
            $data = $_POST;
            $this->validate("old_password",$data['old_password'],'请输入原密码',1);
            $this->validate("new_password",$data['new_password'],'请输入新密码',1);
            $this->validate("new_password1",$data['new_password1'],'请输入确认密码',1);

            if(strlen($data['new_password']) < 6){
                $this->error('密码长度不足6位');
            }
            if($data['new_password'] != $data['new_password1']){
                $this->error('两次密码不一致');
            }
            $info = $store->where(array('store_id'=>$this->sessionInfo['user_id']))->find();
            if(md5($data['old_password']) != $info['password']){
                $this->error('原密码错误');
            }
            if(md5($data['new_password']) == $info['password']){
                $this->error('不可修改为原密码');
            }

            $save = $store->where(array('user_id'=>$this->sessionInfo['user_id']))->save(array('login_password'=>md5($data['new_password'])));
            if($save){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }


        }else{
            $this->display();
        }
    }


}

function getAllDate($startDate,$endDate){
    $date = '';
    $startTime = strtotime($startDate);
    $endTime = strtotime($endDate);

    $middle = $startTime;

    while ( $middle <= $endTime ) {
        $date .= date('Y-m-d',$middle) . ',';
        $middle = strtotime('+1 day',$middle);
    }
    $date = substr($date,0,strlen($date)-1);
    return $date;
}